'use strict';

const Controller = require('../lib/controller');

module.exports = class StatsController extends Controller {
  async index() {

    if (this.ctx.theUser.groupID != 1) {
      await this.ctx.response.redirectMessage(
        this.ctx.__('error'),
        this.ctx.__('no_rights'),
        this.ctx.__('goback'),
        'javascript:window.history.back();',
        false,
        'errorbox'
      );
      return;
    }

    await this.showVisitorList();
  }

  // Show Visitor List ----------------------------------------------
  async showVisitorList() {
    let arrVisitor = this.ctx.app.connBlog.query('SELECT * FROM blog_visitorrecord ORDER BY vr_time DESC');

    if (!arrVisitor) {
      arrVisitor = [];
    }

    const data = {
      strTitle: this.ctx.__('stats'),
      arrVisitor
    };
    await this.ctx.render('stats/visitorList.ejs', data);
  }
};
